# -*- coding: utf-8 -*-
"""
Created on Sun Jun 25 17:00:21 2023

@author: hua'wei
"""

import cv2
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
from skimage.metrics import structural_similarity as ssim
import sys
        



# #使用PIL库函数读取图片，保障读取出的图片对象的模式为RGB
# def openpecture1(image_name):
#     img = Image.open(image_name)
#     return img
        
#在终端显示图片 image:图片矩阵 image_name:图片名称
def pshow(image,image_name):    
    #转换为RGB模式的图片
    if image_name.endswith('jpg'):  # 如果是jpg，需要进行转换
        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    plt.imshow(image)
    plt.axis('off')
    plt.title('RGB')
    plt.show()

#使用窗口显示图像，img图片矩阵
def show_in_window(img):
    #更改窗口大小，使窗口可调节
    #占满整个屏幕将0换为cv2.WINDOW_NORMAL
    cv2.namedWindow("image", 0)
    cv2.imshow('image',255-img)
    #0表示无限等待用户按键，改为k，则kms后关闭窗口
    cv2.waitKey(0)
    cv2.destroyAllWindows()

#保存图像，img：图片矩阵，name：图片要保存为的名称
def savepeacture(img,name):
    cv2.imwrite(name,img)

#截取部分图像 img：图片矩阵 返回截取好的图片矩阵
def cutpeacture(img):
    #截取图像高1600——2700，宽3500-4500位置
    cat = img[1600:2700, 3500:4500]
    return cat
    
#获取图像大小（shape),img:图像矩阵 返回值：图像高、宽、通道数（RGB为3个通道）
def Getshape(img):
    return img.shape

#将图像转为灰度格式并二值化
def binaryzation(img):
    # 将图像转换为灰度格式
    img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    # 二值化
    ret, thresh = cv2.threshold(img_gray, 150, 255, cv2.THRESH_BINARY)
    return thresh


image1 = cv2.imread('question.png')
iamge1 = binaryzation(image1)
image2 = cv2.imread('answer.jpg')
image2 = binaryzation(image2)

img = np.zeros((s[0],s[1]))
s = Getsize(image1)
#找第一行相同的白色像素开始去除

#找第一行不同的白色像素开始复制

#找第二行相同的白色像素结束复制，输出图片然后开始去除

#

print(s)
for i in range(0,s[0]):
    same(image1[i],image2[i])
    for j in range(0,s[1]):
        if (image1[i][j] != 0 ,image2[i][j] != 0):
            

for 
pixel_diff = cv2.absdiff(image1, image2)
pixel_diff = binaryzation(pixel_diff)











image_name = "yes.png"

#img = cv2.imread(image_name)
#pshow(img,image_name)
#show_in_window(img)

#此处为覆盖保存，自动覆盖同名文件
#name='mysife.png'
#savepeacture(img, name)

#截取测试
#cat = cutpeacture(img)
#pshow(cat,image_name)


#



#show_in_window(pixel_diff)
s = Getshape(pixel_diff)
#print(type(pixel_diff))
img = np.zeros((s[0],s[1]))

print(s)
idx = 0
for i in range(0,s[0]):
    flag = 0
    for j in range(0,s[1]):
        if pixel_diff[i][j] != 0:
            flag = 1
            break
    if flag == 1:
        img[idx] = pixel_diff[i]
        idx=idx + 1

img.resize(s[0],idx+10)
show_in_window(img)
        
            

#savepeacture(pixel_diff, 'image.jpg')
# image = cv2.imread('white.png')
# print(image)

